package com.yatb.trac.controller;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Arrays;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.yatb.trac.pojo.StoricoTicket;
import com.yatb.trac.pojo.Tickets;
import com.yatb.trac.pojo.comparator.OrderBy;
import com.yatb.trac.pojo.comparator.StoricoTicketOrderByDataModifica;
import com.yatb.trac.service.SearchService;

public class CreateEmailContentController extends AbstractController {

	private SearchService searchService;
	private String view;
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		InputStream in = request.getInputStream();
		
		ObjectInputStream obj = new ObjectInputStream(in);
		Tickets t = (Tickets) obj.readObject();
		
		//ordino l'elenco storico
		Set<StoricoTicket> setStorico = t.getStoricoTickets();
		StoricoTicket[] st = setStorico.toArray(new StoricoTicket[0]);
		Arrays.sort(st, new StoricoTicketOrderByDataModifica(OrderBy.DESC));

		
		StoricoTicket[] lastStorico = getSearchService().findMaxStoricoTicketByTicket(
				new StoricoTicket[0], t);
		request.setAttribute("lastStorico", lastStorico[0]);
		request.setAttribute("ticket", t);
		request.setAttribute("storico", st);
		ModelAndView mav = new ModelAndView(getView());
		return mav;
	}
	public SearchService getSearchService() {
		return searchService;
	}
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
	public String getView() {
		return view;
	}
	public void setView(String view) {
		this.view = view;
	}

}
